<template>
    <div class="publicpageview">
        <div class="publicformview">
            <el-form :inline="true" :model="listQuery" size="mini" ref="searchForm" label-width="100px">
                <el-row :gutter="20">

                    <el-col :span="4">
                        <el-form-item label="标题：" prop="subject">
                            <el-input v-model="listQuery.subject" placeholder="请输入标题"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="政策类别：" prop="policyCatagory">

                            <el-input v-model="listQuery.policyCatagory" placeholder="请输入政策类别"></el-input>
                        </el-form-item>
                    </el-col>



                    <el-col :span="4">
                        <el-form-item prop="policyLevel" label="政策级别：">
                            <el-input v-model="listQuery.policyLevel" placeholder="请输入政策级别"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <div style="padding-left: 90px;">
                            <el-button size="mini" type="primary" @click="handleSearch">查询</el-button>
                            <el-button size="mini" @click="resetForm('searchForm')">重置</el-button>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <div class="publitableview">
            <el-button type="primary" size="mini" @click="handleAddUser('add')">录入</el-button>
            <el-table :data="list" border fit highlight-current-row size="mini">

                <el-table-column align="center" label="标题" prop="subject">
                </el-table-column>
                <el-table-column align="center" label="政策级别" prop="policyLevel">
                </el-table-column>
                <el-table-column align="center" label="政策分类" prop="policyCatagory">
                </el-table-column>
                <el-table-column align="center" label="创建日期" prop="sendTime">
                </el-table-column>

                <el-table-column align="center" label="操作" width="200" fixed="right">
                    <template slot-scope="scope">

                        <el-button type="text" size="mini" @click="handleUpdate(scope.row, 'update')">编辑</el-button>

                        <el-button type="text" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="publicpaginationview">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="listQuery.pageNum" background :page-sizes="[10, 20, 50, 100]"
                    :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
                    small></el-pagination>
            </div>
        </div>

        <el-dialog :title="isAdd == 'add' ? '录入' :  '修改'" :visible.sync="dialogVisible" :before-close="handleClose">
            <el-form :model="formData" ref="dataForm" class="demo-ruleForm" label-position="right" label-width="150px"
                size="mini">
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item label="标题：" prop="subject">
                            <el-input v-model="formData.subject"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="政策级别：" prop="policyLevel">
                            <el-input v-model="formData.policyLevel"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="政策分类：" prop="policyCatagory">
                            <el-input v-model="formData.policyCatagory"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="发布日期：" prop="sendTime">
                            <el-date-picker clearable v-model="formData.sendTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择发布日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="内容：" prop="content">
                            <quill-editor v-model="formData.content" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="附件：" prop="isPlanSubmission">
                            <el-upload class="upload-demo" :action="uploadAction" :on-remove="handleRemove" multiple
                                :file-list="zhanfileList" :on-success="handleUploadSuccess" :on-preview="handlePreview"
                                :on-error="handleError">
                                <el-button size="small" type="text">点击上传</el-button>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleSubmitClick" size="medium">保 存</el-button>
            </span>
        </el-dialog>




    </div>
</template>

<script>
import { createpolicy, updatepolicy, deletePolicy, policyList } from '@/api/public'
import moment from 'moment';
export default {
    name: "AccommodationManagementIndex",

    data() {
        return {
            listQuery: {
                pageNum: 1,
                pageSize: 20,
                subject: "",
                policyLevel: "",
                policyCatagory: ""
            },
            total: 0,
            list: [],


            dialogVisible: false,

            isAdd: 'add',
            formData: {
                subject: "",
                policyCatagory: null,
                policyLevel: null,
                content: null,
                file: null,
                submitTime: null
            },
            zhanfileList: [],
            uploadAction: 'http://125.39.77.18:9021/mall-admin/fileUpload/upload',

        };
    },

    computed: {

    },

    mounted() {

        this.getList();
    },

    methods: {

        handleSearch() {
            this.listQuery.pageNum = 1;
            this.getList();
        },



        handleRemove(file, fileList) {
            this.zhanfileList = fileList
        },

        handleError(err, file, fileList) {
            console.log(err);
            console.log(fileList)
        },

        handlePreview(file) {
            if (file.response) {
                window.open(file.response.data.url, '_blank');
            } else {
                window.open(file.url, '_blank');
            }

        },


        handleUploadSuccess(response, file, fileList) {
            let arr = [];

            fileList.forEach(item => {
                if (item.response && item.response.code == 200) {
                    arr.push(item)
                } else if (item.response && item.response != 200) {
                    this.$message.error(item.name + item.response.message)
                } else {
                    arr.push(item)
                }
            })
            this.zhanfileList = arr
        },


        handleSizeChange(val) {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.listQuery.pageNum = val;
            this.getList();
        },
        getList() {

            policyList(this.listQuery).then((response) => {
                this.total = response.data.total;
                this.list = response.data.list;

            });
        },

        resetForm(val) {

            this.$refs[val].resetFields();
            this.zhanfileList = [];
        },

        handleAddUser(type) {
            this.isAdd = 'add';
            this.dialogVisible = true;
        },

        handleClose() {
            this.resetForm("dataForm");
            this.dialogVisible = false;

        },

        handleSubmitClick() {
            const { formData } = this;
            if (this.isAdd == 'add') {
                let arr = []
                if (this.zhanfileList.length > 0) {
                    this.zhanfileList.forEach(item => {
                        arr.push({ url: item.response.data.url, name: item.response.data.name })
                    })
                }
                createpolicy({ ...formData, file: JSON.stringify(arr) }).then(res => {
                    this.handleClose()
                    this.getList();
                    this.$message.success("操作成功")
                })
            }
            if (this.isAdd == 'update') {

                let arr = []
                if (this.zhanfileList.length > 0) {
                    this.zhanfileList.forEach(item => {
                        if (item.response) {
                            arr.push({ url: item.response.data.url, name: item.response.data.name })
                        } else {
                            arr.push({ url: item.url, name: item.name })
                        }
                    })
                }
                updatepolicy({ ...formData, file: JSON.stringify(arr) }).then(res => {
                    this.handleClose()
                    this.getList();
                    this.$message.success("操作成功")

                })
            }

        },



        handleUpdate(currentData, type) {
            this.isAdd = type;
            //   一定要在打开弹窗前 再赋值 不然resetFiles没用
            this.dialogVisible = true;
            let str = currentData.file;
            if (str) {
                this.zhanfileList = JSON.parse(str);
            }
            this.$nextTick(() => {
                this.formData = { ...currentData };
            });
        },



        handleDelete(id) {
            this.$confirm(
                "此操作将删除该条内容，请确认是否继续操作？",
                "删除提示",
                {
                    confirmButtonText: "保 存",
                    cancelButtonText: "取 消",
                    type: "warning",
                }
            )
                .then(() => {
                    deletePolicy(id).then(res => {
                        this.$message.success("操作成功")
                        this.getList();
                    })
                })
                .catch(() => { });
        }
    },
};
</script>